package com.example.demo.service.Impl;

import com.example.demo.config.LoginHandlerInterceptor;
import com.example.demo.mapper.LifebookMapper;
import com.example.demo.mapper.YanWangMapper;
import com.example.demo.pojo.Lifebook;
import com.example.demo.pojo.LogTest;
import com.example.demo.pojo.YanWang;
import com.example.demo.service.YanWangService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class YanWangServiceImpl implements YanWangService {
    @Autowired
    private YanWangMapper yanWangMapper;
    @Autowired
    private LifebookMapper lifebookMapper;
    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;
    @Override
    public String trial(YanWang yanWang) {
        Lifebook lifebook = lifebookMapper.selectByid(yanWang.getLifebook_id());
        if (lifebook.getReaperid() == null) {
            return "还未勾魂";
        }else {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            yanWang.setUuid(uuid);
            yanWang.setLifebook_id(lifebook.getId());
            yanWang.setReaper_id(lifebook.getReaperid());
            LogTest logTest = loginHandlerInterceptor.UserTokenLog();
            lifebook.setJudgeid(logTest.getId());
            yanWang.setJudge_id(lifebook.getJudgeid());
            yanWangMapper.trial(yanWang);
            lifebookMapper.updateLifeBook(lifebook);
            return "审判成功";
        }
    }

    @Override
    public YanWang getJudgementDetail(String uuid) {
        return yanWangMapper.getJudgementDetail(uuid);
    }



    @Override
    public Integer getJudgementListCount() {
        return yanWangMapper.getJudgementListCount();
    }

    @Override
    public PageInfo getJudgementList(Integer page, Integer limit,YanWang yanWang) {
        PageHelper.startPage(page,limit);
        List<YanWang> list = yanWangMapper.getJudgementList(yanWang);
        PageInfo<YanWang> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void deleteJudgement(String uuid) {
        yanWangMapper.deleteJudgement(uuid);
    }

    @Override
    public void updateJudgement(YanWang yanWang) {
        yanWangMapper.updateJudgement(yanWang);
    }
}
